import time
from unittest import TestCase

import pymysql
from selenium import webdriver
from selenium.webdriver.common.by import By


class TestBuy(TestCase):
    def test_zfblqlc(self):
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = "select username from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        self.assertIn(('lisi',), x)
        sql = "select username,id from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        for i in range(len(x)):
            if 'lisi' in x[i - 1]:
                userid = x[i - 1][1]
        sql = f"update bankcard set balance=100000 where userId={userid} and defaultl=1"
        cursor.execute(sql)
        connect.commit()
        sql = f"select balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        money = x[0][0]
        connect.commit()

        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.maximize_window()
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi")
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[1]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,
                                  '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[6]/button').click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '/html/body/div[3]/div[2]/input').send_keys('666666')

        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
        time.sleep(1)
        x = chromeDriver.find_element(By.XPATH,
                                      '/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        print(x)
        self.assertEqual(x, '买入成功!')
        time.sleep(3)
        sql = f"select balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        new_money = x[0][0]
        self.assertEqual(money - 100, new_money)

    def test_zfblqlc_password(self):
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = "select username from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        self.assertIn(('lisi',), x)
        sql = "select username,id from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        for i in range(len(x)):
            if 'lisi' in x[i - 1]:
                userid = x[i - 1][1]
        sql = f"select balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        money = x[0][0]
        connect.commit()
        self.assertGreaterEqual(money, 100)

        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.maximize_window()
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi")
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[1]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,
                                  '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[6]/button').click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '/html/body/div[3]/div[2]/input').send_keys('666667')

        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
        time.sleep(1)
        x = chromeDriver.find_element(By.XPATH,
                                      '/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        print(x)
        self.assertEqual(x, '买入失败!')
        time.sleep(3)
        sql = f"select balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        new_money = x[0][0]
        self.assertEqual(money, new_money)

    def test_zfblqlc_yue(self):
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = "select username from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        self.assertIn(('lisi',), x)
        sql = "select username,id from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        for i in range(len(x)):
            if 'lisi' in x[i - 1]:
                userid = x[i - 1][1]
        sql = f"update bankcard set balance=99 where userId={userid} and defaultl=1"
        cursor.execute(sql)
        connect.commit()
        sql = f"select balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        money = x[0][0]
        connect.commit()
        self.assertGreaterEqual(100, money)

        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.maximize_window()
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi")
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[1]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,
                                  '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[6]/button').click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '/html/body/div[3]/div[2]/input').send_keys('666666')

        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
        time.sleep(1)
        x = chromeDriver.find_element(By.XPATH,
                                      '/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        print(x)
        self.assertEqual(x, '买入失败!，银行卡余额不足')
        time.sleep(3)
        sql = f"select balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        new_money = x[0][0]
        self.assertEqual(money, new_money)

    def test_zfblqlc_wbd(self):
        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.maximize_window()
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi5")
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[1]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,
                                  '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[6]/button').click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '/html/body/div[3]/div[2]/input').send_keys('666666')

        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
        time.sleep(1)
        x = chromeDriver.find_element(By.XPATH,
                                      '/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        print(x)
        self.assertEqual(x, '买入失败!，请绑定银行卡')

    def test_qxlc(self):
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = "select username from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        self.assertIn(('lisi',), x)
        sql = "select username,id from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        for i in range(len(x)):
            if 'lisi' in x[i - 1]:
                userid = x[i - 1][1]
        sql = f"update bankcard set balance=100000 where userId={userid} and defaultl=1"
        cursor.execute(sql)
        connect.commit()
        sql = f"select balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        money = x[0][0]
        connect.commit()

        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.maximize_window()
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi")
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,
                                  '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[8]/button').click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '//html/body/div[3]/div[2]/input').send_keys('666666')

        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
        time.sleep(1)
        x = chromeDriver.find_element(By.XPATH,
                                      '//html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        print(x)
        self.assertEqual(x, '买入成功!')
        time.sleep(3)
        sql = f"select balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        new_money = x[0][0]
        self.assertEqual(money - 5000, new_money)

    def test_qxlc_password(self):
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = "select username from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        self.assertIn(('lisi',), x)
        sql = "select username,id from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        for i in range(len(x)):
            if 'lisi' in x[i - 1]:
                userid = x[i - 1][1]
        sql = f"select balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        money = x[0][0]
        connect.commit()
        self.assertGreaterEqual(money, 100)

        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.maximize_window()
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi")
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,
                                  '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[8]/button').click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '/html/body/div[3]/div[2]/input').send_keys('666667')

        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
        time.sleep(1)
        x = chromeDriver.find_element(By.XPATH,
                                      '/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        print(x)
        self.assertEqual(x, '买入失败!')
        time.sleep(3)
        sql = f"select balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        new_money = x[0][0]
        self.assertEqual(money, new_money)

    def test_qxlc_yue(self):
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = "select username from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        self.assertIn(('lisi',), x)
        sql = "select username,id from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        for i in range(len(x)):
            if 'lisi' in x[i - 1]:
                userid = x[i - 1][1]
        sql = f"update bankcard set balance=99 where userId={userid} and defaultl=1"
        cursor.execute(sql)
        connect.commit()
        sql = f"select balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        money = x[0][0]
        connect.commit()
        self.assertGreaterEqual(100, money)

        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.maximize_window()
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi")
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,
                                  '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[8]/button').click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '/html/body/div[3]/div[2]/input').send_keys('666666')

        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
        time.sleep(1)
        x = chromeDriver.find_element(By.XPATH,
                                      '/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        print(x)
        self.assertEqual(x, '买入失败!，银行卡余额不足')
        time.sleep(3)
        sql = f"select balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        new_money = x[0][0]
        self.assertEqual(money, new_money)

    def test_qxlc_wbd(self):
        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.maximize_window()
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi5")
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,
                                  '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[8]/button').click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '/html/body/div[3]/div[2]/input').send_keys('666666')

        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
        time.sleep(1)
        x = chromeDriver.find_element(By.XPATH,
                                      '/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        print(x)
        self.assertEqual(x, '买入失败!，请绑定银行卡')

    def test_jjlc(self):
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = "select username from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        self.assertIn(('lisi',), x)
        sql = "select username,id from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        for i in range(len(x)):
            if 'lisi' in x[i - 1]:
                userid = x[i - 1][1]
        sql = f"update bankcard set balance=100000 where userId={userid} and defaultl=1"
        cursor.execute(sql)
        connect.commit()
        sql = f"select balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        money = x[0][0]
        connect.commit()

        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.maximize_window()
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi")
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[3]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,
                                  '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[9]/button').click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '/html/body/div[3]/div[2]/input').send_keys('666666')

        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
        time.sleep(1)
        x = chromeDriver.find_element(By.XPATH,
                                      '/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        print(x)
        self.assertEqual(x, '买入成功!')
        time.sleep(3)
        sql = f"select balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        new_money = x[0][0]
        self.assertEqual(money - 3000, new_money)

    def test_jjlc_password(self):
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = "select username from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        self.assertIn(('lisi',), x)
        sql = "select username,id from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        for i in range(len(x)):
            if 'lisi' in x[i - 1]:
                userid = x[i - 1][1]
        sql = f"select balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        money = x[0][0]
        connect.commit()
        self.assertGreaterEqual(money, 100)

        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.maximize_window()
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi")
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[3]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,
                                  '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[9]/button').click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '/html/body/div[3]/div[2]/input').send_keys('666667')

        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
        time.sleep(1)
        x = chromeDriver.find_element(By.XPATH,
                                      '/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        print(x)
        self.assertEqual(x, '买入失败!')
        time.sleep(3)
        sql = f"select balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        new_money = x[0][0]
        self.assertEqual(money, new_money)

    def test_jjlc_yue(self):
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = "select username from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        self.assertIn(('lisi',), x)
        sql = "select username,id from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        for i in range(len(x)):
            if 'lisi' in x[i - 1]:
                userid = x[i - 1][1]
        sql = f"update bankcard set balance=99 where userId={userid} and defaultl=1"
        cursor.execute(sql)
        connect.commit()
        sql = f"select balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        money = x[0][0]
        connect.commit()
        self.assertGreaterEqual(100, money)

        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.maximize_window()
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi")
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[3]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,
                                  '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[9]/button').click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '/html/body/div[3]/div[2]/input').send_keys('666666')

        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
        time.sleep(1)
        x = chromeDriver.find_element(By.XPATH,
                                      '/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        print(x)
        self.assertEqual(x, '买入失败!，银行卡余额不足')
        time.sleep(3)
        sql = f"select balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        new_money = x[0][0]
        self.assertEqual(money, new_money)

    def test_jjlc_wbd(self):
        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.maximize_window()
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi5")
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[3]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,
                                  '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[9]/button').click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '/html/body/div[3]/div[2]/input').send_keys('666666')

        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
        time.sleep(1)
        x = chromeDriver.find_element(By.XPATH,
                                      '/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        print(x)
        self.assertEqual(x, '买入失败!，请绑定银行卡')


class TestZhiFu(TestCase):
    def test_nopassword(self):
        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.maximize_window()
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi6")
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[3]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,
                                  '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[9]/button').click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '/html/body/div[3]/div[2]/input').send_keys('666666')

        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
        time.sleep(1)
        x = chromeDriver.find_element(By.XPATH,
                                      '/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        self.assertEqual(x, '买入失败!,请前往个人信息设置支付密码')

    def test_default(self):
        # 获取三张银行卡号和余额
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = "select username,id from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        for i in range(len(x)):
            if 'lisi7' in x[i - 1]:
                userid = x[i - 1][1]
        sql = f"select cardNum,balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        card1 = x[0][0]
        money1 = x[0][1]
        connect.commit()
        sql = f"select cardNum,balance from bankcard where userId={userid} and defaultl=0"
        cursor.execute(sql)
        x = cursor.fetchall()
        card2 = x[0][0]
        money2 = x[0][1]
        card3 = x[1][0]
        money3 = x[1][1]
        connect.commit()
        # 买东西
        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.maximize_window()
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi7")
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[3]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,
                                  '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[9]/button').click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '/html/body/div[3]/div[2]/input').send_keys('666666')

        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
        time.sleep(1)
        x = chromeDriver.find_element(By.XPATH,
                                      '/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        self.assertEqual(x, '买入成功!')
        # 对比余额
        sql = f"select cardNum,balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        new_money1 = x[0][1]
        connect.commit()
        sql = f"select cardNum,balance from bankcard where userId={userid} and defaultl=0"
        cursor.execute(sql)
        x = cursor.fetchall()
        new_money2 = x[0][1]
        new_money3 = x[1][1]
        connect.commit()
        print(money1 - 3000)
        self.assertEqual(money1 - 3000, new_money1)
        self.assertEqual(money2, new_money2)
        self.assertEqual(money3, new_money3)

    def test_changed_efault(self):
        # 获取三张银行卡号和余额
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = "select username,id from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        for i in range(len(x)):
            if 'lisi7' in x[i - 1]:
                userid = x[i - 1][1]
        sql = f"select cardNum,balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        card1 = x[0][0]
        money1 = x[0][1]
        connect.commit()
        sql = f"select cardNum,balance from bankcard where userId={userid} and defaultl=0"
        cursor.execute(sql)
        x = cursor.fetchall()
        card2 = x[0][0]
        money2 = x[0][1]
        card3 = x[1][0]
        money3 = x[1][1]
        connect.commit()
        # 改默认
        chromeDriver = webdriver.Chrome()
        chromeDriver.get("http://localhost:90")
        chromeDriver.maximize_window()
        chromeDriver.find_element(By.XPATH, "//input[@id='username']").send_keys("lisi7")
        chromeDriver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        chromeDriver.find_element(By.ID, "login_btn").click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div[3]/div/div/div[1]/ul/li/button[3]').click()
        time.sleep(2)
        connect.commit()
        #判断卡变没变
        sql = f"select cardNum,balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        newcard1 = x[0][0]
        connect.commit()
        sql = f"select cardNum,balance from bankcard where userId={userid} and defaultl=0"
        cursor.execute(sql)
        x = cursor.fetchall()
        newcard2 = x[0][0]
        newcard3 = x[1][0]
        connect.commit()
        self.assertEqual(card1,newcard2)
        self.assertEqual(card2,newcard1)
        self.assertEqual(card3,newcard3)
        # 买东西
        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[3]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH,
                                  '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[9]/button').click()
        time.sleep(1)

        chromeDriver.find_element(By.XPATH, '/html/body/div[3]/div[2]/input').send_keys('666666')

        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
        time.sleep(1)
        x = chromeDriver.find_element(By.XPATH,
                                      '/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
        self.assertEqual(x, '买入成功!')
        # 对比余额
        sql = f"select cardNum,balance from bankcard where userId={userid} and defaultl=1"
        cursor.execute(sql)
        x = cursor.fetchall()
        new_money1 = x[0][1]
        connect.commit()
        sql = f"select cardNum,balance from bankcard where userId={userid} and defaultl=0"
        cursor.execute(sql)
        x = cursor.fetchall()
        new_money2 = x[0][1]
        new_money3 = x[1][1]
        connect.commit()
        self.assertEqual(money1, new_money2)
        self.assertEqual(money2-3000, new_money1)
        self.assertEqual(money3, new_money3)
